import { JavascriptGenerator, javascriptGenerator } from "blockly/javascript";
import { Tanslate } from "./translate";
import * as Blockly from 'blockly/core';
import { getValue } from "./com";
export namespace Variables {
    const blocks = [
        {
            "key": "variables",
            "message0": "%{variables} %1",
            "args0": [
                {
                    "type": "field_variable",
                    "name": "name",
                    "variable": "var1"
                },
            ],
            "output": null,
            "colour": 310
        },
        {
            "key": "new_variables",
            "message0": "%{new_variables} %1",
            "args0": [
                {
                    "type": "field_variable",
                    "name": "name",
                    "variable": "_temp_variable_2974921ascashajj099cm"
                },
            ],
            "nextStatement": null,
            "previousStatement": null,
            "colour": 310
        },
        {
            "key": "new_variables_assign",
            "message0": "%{new_variables_assign} %1 %2",
            "args0": [
                {
                    "type": "field_variable",
                    "name": "name",
                    "variable": "_temp_variable_2974921ascashajj099cm"
                },
                {
                    "type": "input_value",
                    "name": "value",
                },
            ],
            "nextStatement": null,
            "previousStatement": null,
            "colour": 310
        },
        {
            "key": "set_variables",
            "message0": "%{assign} %1 %{to} %2",
            "args0": [
                {
                    "type": "input_value",
                    "name": "value"
                },
                {
                    "type": "field_variable",
                    "name": "name",
                    "variable": "var1"
                },
            ],
            "nextStatement": null,
            "previousStatement": null,
            "colour": 310
        },
        {
            "key": "variables_is_invalid",
            "message0": "%{variables_is_invalid} %1",
            "args0": [
                {
                    "type": "input_value",
                    "name": "VAR"
                },
            ],
            "output": null,
            "colour": 310
        },
    ]

    export function init() {
        initGenerator()
        Tanslate.tr(blocks).forEach((item: any) => {
            Blockly.Blocks[item.key] = {
                init: function (this: Blockly.Block) {
                    this.jsonInit(item);
                }
            }
        })
    }

    export function getTools() {
        const netBlocks = {
            kind: 'category',
            name: Blockly.Msg['variables'],
            "colour": 310,
            contents: [] as any[]
        }
        blocks.forEach((item: any) => {
            netBlocks.contents.push({
                kind: 'block',
                type: item.key
            })
        })

        return netBlocks
    }

    function initGenerator() {
        javascriptGenerator.finish = (code:string) => {
            return code
        };

        javascriptGenerator.forBlock['variables'] = function (block: Blockly.Block, generator: JavascriptGenerator) {
            const name = getValue(block, generator, 'name')
            if (!name) {
                throw new Error(block.id);
            }
            return `${name}`;
        }
        javascriptGenerator.forBlock['new_variables'] = function (block: Blockly.Block, generator: JavascriptGenerator) {
            const name = getValue(block, generator, 'name')
            if (!name) {
                throw new Error(block.id);
            }

            if (!/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name)) {
                throw new Error(block.id);
            }
            return `let ${name};\n`;
        }

        javascriptGenerator.forBlock['new_variables_assign'] = function (block: Blockly.Block, generator: JavascriptGenerator) {
            const name = getValue(block, generator, 'name')
            const value = getValue(block, generator, 'value')?.trim();
            if (!name) {
                throw new Error(block.id);
            }

            if (!/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name)) {
                throw new Error(block.id);
            }
            return `let ${name} = ${value};\n`;
        }

        javascriptGenerator.forBlock['set_variables'] = function (block: Blockly.Block, generator: JavascriptGenerator) {
            const name = getValue(block, generator, 'name')
            const value = getValue(block, generator, 'value')
            if (!name) {
                throw new Error(block.id);
            }
            return `${name} = ${value};\n`;
        }

        javascriptGenerator.forBlock['variables_is_invalid'] = function (block: Blockly.Block, generator: JavascriptGenerator) {
            const VAR = getValue(block, generator, 'VAR');
            if (!VAR) {
                throw new Error(block.id);
            }
            return `(${VAR}===undefined || ${VAR}===null)`;
        }
    }
}